L.TileLayer.ChinaProvider = L.TileLayer.extend({
    initialize: function (type, options) {
        // (type, Object)
        var providers = L.TileLayer.ChinaProvider.providers;

        var parts = type.split(".");

        var providerName = parts[0];
        var mapName = parts[1];
        var mapType = parts[2];

        var url = providers[providerName][mapName][mapType];
        options.subdomains = providers[providerName].Subdomains;

        L.TileLayer.prototype.initialize.call(this, url, options);
    },
});

L.TileLayer.ChinaProvider.providers = {
    TianDiTu: {
        Normal: {
            Map: "https://t{s}.tianditu.gov.cn/DataServer?T=vec_w&X={x}&Y={y}&L={z}&tk=eec7fae87888db2dc4c30c0c458fb66b",
            Annotion:
                "https://t{s}.tianditu.gov.cn/DataServer?T=cva_w&X={x}&Y={y}&L={z}&tk=eec7fae87888db2dc4c30c0c458fb66b",
        },
        Satellite: {
            Map: "https://t{s}.tianditu.gov.cn/DataServer?T=img_w&X={x}&Y={y}&L={z}&tk=eec7fae87888db2dc4c30c0c458fb66b",
            Annotion:
                "https://t{s}.tianditu.gov.cn/DataServer?T=cva_w&X={x}&Y={y}&L={z}&tk=eec7fae87888db2dc4c30c0c458fb66b",
        },
        Terrain: {
            Map: "https://t{s}.tianditu.gov.cn/DataServer?T=ter_w&X={x}&Y={y}&L={z}&tk=eec7fae87888db2dc4c30c0c458fb66b",
            Annotion:
                "https://t{s}.tianditu.gov.cn/DataServer?T=cva_w&X={x}&Y={y}&L={z}&tk=eec7fae87888db2dc4c30c0c458fb66b",
        },
        Subdomains: ["0", "1", "2", "3", "4", "5", "6", "7"],
    },
};

L.tileLayer.chinaProvider = function (type, options) {
    return new L.TileLayer.ChinaProvider(type, options);
};
